package com.sicau.basedemo.util;


import com.sicau.basedemo.entity.param.PageParam;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * 生成insertBatch的sql的工具类
 * @author xiongtao29
 */
public class BatchSqlGenerator {

    public static <T> String getBatchSqlStatement(Class<T> clazz, List<T> list){

        StringBuilder stringBuilder = new StringBuilder("INSERT into ");
        String clazzName = UnderlineWithHump.HumpToUnderline(clazz.getSimpleName());
        stringBuilder.append(clazzName);

        //获取方法
        Method[] declaredMethods = clazz.getDeclaredMethods();
        Stream<Method> getMethods = Arrays.stream(declaredMethods).filter(item -> item.getName().startsWith("get"));

        //获取column名称
        List<String> columnList = new ArrayList<>();
        getMethods.forEach(item -> {
            //转换为下划线命名
            String methodName = item.getName();
            String tmp = methodName.substring(methodName.indexOf("get") + "get".length());
            columnList.add(UnderlineWithHump.HumpToUnderline(tmp));
        });

        //拼接values前面的括号
        int columnListSize = columnList.size();
        stringBuilder.append("(");
        //for-1
        for (int i=0;i<columnListSize;i++){
            stringBuilder.append(columnList.get(i));
            if (i!=columnListSize-1){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append(") VALUES");

        //for-2
        for (int j=0;j<list.size();j++){
            //map的size应该和columnList一样
            Map<String, Object> map = UnderlineWithHump.ObjectFieldNameToUnderline(list.get(j));

            //此处的for-3循环顺序必须和for-1的一致！！！
            //for-3
            stringBuilder.append("(");
            for (int i=0;i<columnListSize;i++){

                Object value = map.get(columnList.get(i));
                //判断是否是localDateTime
                if (value instanceof LocalDateTime){
                    String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format((LocalDateTime) value);
                    stringBuilder.append("'");
                    stringBuilder.append(format);
                    stringBuilder.append("'");
                }else if (value instanceof String){
                    stringBuilder.append("'");
                    stringBuilder.append(map.get(columnList.get(i)));
                    stringBuilder.append("'");
                }else {
                    stringBuilder.append(map.get( columnList.get(i) ));
                }


                if (i!=columnListSize-1){
                    stringBuilder.append(",");
                }
            }
            if (j!=list.size()-1){
                stringBuilder.append("),");
            }
        }
        stringBuilder.append(");");

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        PageParam param = new PageParam();
        List<PageParam> list = new ArrayList<>();
        list.add(param);
        list.add(param);
        list.add(param);
        String batchSqlStatement = getBatchSqlStatement(PageParam.class,list);
        System.out.println(batchSqlStatement);
    }
}
